﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace tjb.ConsoleTest
{
    class 泛型工厂
    {
    }
    public interface IFactory<T>
    {
        T Create();
    } 
    public sealed class TypeToken<T>
    {
        static private TypeToken<T> instanceValue = new TypeToken<T>();
        static public TypeToken<T> Instance
        {
            get { return instanceValue; }
        }

        private TypeToken() { }
    }
    public interface IAbstractFactory<T1, T2>
    {
        T1 Create(TypeToken<T1> token);
        T2 Create(TypeToken<T2> token);
    }
    public class ConcreteFactory<T1, T2> : IAbstractFactory<T1, T2>
    {
        private IFactory<T1> factory1;
        private IFactory<T2> factory2;

        public ConcreteFactory(IFactory<T1> f1, IFactory<T2> f2)
        {
            factory1 = f1;
            factory2 = f2;
        }

        public T1 Create(TypeToken<T1> token)
        {
            return factory1.Create();
        }

        public T2 Create(TypeToken<T2> token)
        {
            return factory2.Create();
        }
    }

    public static class ConcretFactory
    {
        public static ConcreteFactory<T1, T2> NewFactory<T1, T2>(IFactory<T1> f1, IFactory<T2> f2)
        {
            return new ConcreteFactory<T1, T2>(f1, f2);
        }
    } 
}
